﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using iShoppingBUS;
using iShoppingDTO;

namespace iShopping.Admin.UserControl
{
    public partial class ThemTaiKhoan : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = DateTime.Now.Year; i > 1891; i--)
                {
                    dlNam.Items.Add(i.ToString());
                }

                List<CityDTO> list = CityBUS.getCitys();
                for (int i = 0; i < list.Count; i++)
                {
                    dlNoiSinh.Items.Add(list[i].Ten);
                }
                DataTable dt = LoaiTaiKhoanBUS.SelectAllLoai();
                dlLoai.DataSource = dt;
                dlLoai.DataTextField = "TenLoaiTK";
                dlLoai.DataValueField = "MaLoaiTK";
                dlLoai.DataBind();
            }
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            DateTime date = DateTime.Now;
            long ticks = date.Ticks;
            string ma = ticks.ToString();

            TaiKhoanDTO taiKhoanDto = new TaiKhoanDTO();
            taiKhoanDto.MaTaiKhoan = ma;
            taiKhoanDto.TenTaiKhoan = txtTenTaiKhoan.Text.Trim();
            taiKhoanDto.MatKhau = txtMatKhau.Text.Trim();
            taiKhoanDto.MaLoaiTK = dlLoai.SelectedItem.Value.Trim();
            taiKhoanDto.Avatar = "~/images/user.png";
            taiKhoanDto.TrangThai = "UnLock";
            taiKhoanDto.DiemTinCayBan = "0";
            taiKhoanDto.DiemTinCayMua = "0";

            HttpPostedFile postFile = FileUpload1.PostedFile;
            if (postFile.FileName != "")
            {
                if (!File.Exists(Server.MapPath("~\\images") + "\\" + Path.GetFileName(postFile.FileName)))
                {
                    postFile.SaveAs(Server.MapPath("~\\images") + "\\" + Path.GetFileName(postFile.FileName));
                }

                taiKhoanDto.Avatar = "~\\images" + "\\" + Path.GetFileName(postFile.FileName);
              
            }
            if(!TaiKhoanBUS.checkTenTaiKhoan(txtTenTaiKhoan.Text.Trim()))
            {
                bool result = TaiKhoanBUS.InsertAllTaiKhoan(taiKhoanDto);

                if (result)
                {
                    ThongTinTaiKhoanDTO ttTK = new ThongTinTaiKhoanDTO();
                    ttTK.MaTaiKhoan = taiKhoanDto.MaTaiKhoan;
                    ttTK.HoTen = txtTen.Text.Trim();
                    ttTK.DiaChi = txtDiaChi.Text.Trim();
                    ttTK.NoiSinh = dlNoiSinh.SelectedItem.Value.Trim();
                    ttTK.MaTheTinDung = txtMaThe.Text.Trim();
                    ttTK.SoDienThoai = txtDidong.Text.Trim();
                    ttTK.Email = txEmail.Text.Trim();
                    if (dlGioiTinh.Text.Trim().Equals("Nam"))
                    {
                        ttTK.GioiTinh = "Nam";
                    }
                    if (dlGioiTinh.Text.Trim().Equals("Nữ"))
                    {
                        ttTK.GioiTinh = "Nữ";
                    }
                    int ngay = int.Parse(dlNgay.Items[dlNgay.SelectedIndex].Value);
                    int thang = int.Parse(dlThang.Items[dlThang.SelectedIndex].Value);
                    int nam = int.Parse(dlNam.Items[dlNam.SelectedIndex].Value);
                    DateTime dateTime = new DateTime(nam, thang, ngay);
                    ttTK.NgaySinh = dateTime.ToString();


                    bool res = ThongTinTaiKhoanBUS.insertThongTinTK(ttTK);
                }
            }
          
            


        }
        
    }
}